// pages/order_check_task/order_check_task.ts
export { };
const urls = require("../../utils/urls");
const my_date = require("../../utils/my_date");


class GroupCheckTask {
    "uuid": string;
    "time_start": string;
    "duration": string;
    "sum_num": string;
    "used_num": string;
    "place_uuid": string;
    // 该任务 限制 预约 的 最早时间
    // 用户表 order time 早于 该 时间 则 禁止 预约
    "limit_order_time": string;
    // show attr
    "time_start_show": string;
    "time_end_show": string;
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        group_check_tasks: ([] as GroupCheckTask[]),
        place: "",
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        const place = wx.getStorageSync("select_order_check_place");
        console.log("select_order_check_place");
        console.log(place);
        this.setData({
            place: place
        })
        this.get_group_check_task_by_uuid(place.uuid)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    get_group_check_task_by_uuid(place_uuid: string) {
        let that = this;
        wx.request({
            url: urls.gc_get_tasks_by_place_url,
            data: {
                "place_uuid": place_uuid
            },
            success(res: any) {
                console.log("request get_group_check_task_by_uuid");
                console.log(res);
                let tasks = res.data.tasks;
                for (let [i, v] of tasks.entries()) {
                    v["time_start_show"] = my_date.get_readable_date(v["time_start"]);
                    v["time_end_show"] = my_date.get_readable_date(v["time_end"]);
                    v["index"] = i;
                }
                that.setData({
                    group_check_tasks: tasks,
                });
            }
        })
    },
    order_check_task(e: any) {
        const app_user_info = wx.getStorageSync("app_user_info");
        let task_index = e.currentTarget.dataset.task_index;
        let task = this.data.group_check_tasks[task_index];
        if (Number(task.sum_num) - Number(task.used_num) === 0) {
            wx.showToast({
                title: '无空闲名额',
                icon: 'success',
                duration: 5000,
                mask: true
            });
            return;
        }
        console.log("order_check_task");
        console.log(task);
        // request server order_check_task
        let req_success_callback = (res: any) => {
            console.log("req_success_callback");
            console.log(res);
            let code = res.data.code;
            if (code == "0") {
                setTimeout(() => {
                    wx.navigateBack({ delta: 10 });
                }, 2000);
                wx.showToast({
                    title: '预约成功',
                    icon: 'success',
                    duration: 3000,
                    mask: true
                })
            } else {
                wx.showToast({
                    title: '预约失败',
                    icon: 'success',
                    duration: 2000,
                    mask: true
                })
            }
        };
        let req = () => {
            wx.request({
                url: urls.gc_order_group_check_url,
                method: "POST",
                data: {
                    "user_uuid": app_user_info.uuid,
                    "group_check_uuid": task.uuid,
                },
                success(res: any) {
                    req_success_callback(res);
                }
            })
        }
        wx.showModal({
            title: '提示',
            content: '确定预约' + task.time_start_show + "的核酸检测？",
            success(res) {
                if (res.confirm) {
                    req();
                } else if (res.cancel) {
                    console.log('用户点击取消');
                }
            }
        })
    }
})